package com.zy.mc_module.base;

import androidx.annotation.NonNull;
import androidx.collection.SparseArrayCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.zy.mc_module.utils.NoDoubleClickListener;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.wheel.recyclerview.BaseViewHolder;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * Created on 18/10/23
 * RecyclerView 通用 BaseAdapter
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    public String TAG = getClass().getSimpleName() + "";
    private Class clazz;
    private int layoutId;
    private List<T> mData;
    private OnItemClickListener itemClickListener;
    private OnItemLongClickListener itemLongClickListener;

    public static final int CONTENT_VIEW = 0x00000111;//主视图
    public static final int HEADER_VIEW = 0x00000222;//头部视图
    public static final int FOOTER_VIEW = 0x00000333;//尾部视图
    public static final int EMPTY_VIEW = 0x00000555;//空视图

    /***头部视图***/
    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    /***尾部视图***/
    private SparseArrayCompat<View> mFootViews = new SparseArrayCompat<>();

    /***empty***/
    private FrameLayout mEmptyLayout;

    public BaseAdapter(List<T> lists, int layoutId) {
        this.mData = lists == null ? new ArrayList<>() : lists;
        this.layoutId = layoutId;
        ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz = (Class<T>) parameterizedType.getActualTypeArguments()[0];
    }

    public BaseAdapter(List<T> lists) {
        this.mData = lists == null ? new ArrayList<>() : lists;
        ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz = (Class<T>) parameterizedType.getActualTypeArguments()[0];
    }

    public BaseAdapter() {
        ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz = (Class<T>) parameterizedType.getActualTypeArguments()[0];
    }


    @Override
    public int getItemCount() {
        return getHeadersCount() + getFootersCount() + getRealItemCount();
    }

    /***
     * 判断当前item类型
     */
    @Override
    public int getItemViewType(int position) {
        if (isHeaderViewPos(position)) {
            //头部
            return mHeaderViews.keyAt(position);
        } else if (isFooterViewPos(position)) {
            //尾部
            return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount());
        }
        return CONTENT_VIEW;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //空视图
        if (viewType == EMPTY_VIEW) {
            return new BaseViewHolder(mEmptyLayout);
        }
        //主布局
        View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        //单击事件
        itemView.setOnClickListener(new NoDoubleClickListener() {
            public void onNoDoubleClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick((Integer) v.getTag());
                }
            }
        });
        itemView.setOnLongClickListener(v -> {
            if (itemLongClickListener != null) {
                itemLongClickListener.onItemLongClick((Integer) v.getTag());
            }
            return true;
        });

        if (null != mHeaderViews.get(viewType)) {
            return BaseViewHolder.createViewHolder(mHeaderViews.get(viewType));
        } else if (null != mFootViews.get(viewType)) {
            return BaseViewHolder.createViewHolder(mFootViews.get(viewType));
        }
        return new BaseViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        if (null == mEmptyLayout) {
            //holder.itemView.setTag(position);
            //onMyBindViewHolder(holder, position);

            //如果是HeaderView 或者是 FooterView 不进行绑定数据,因为是从外部传进来的,不由列表更新
            if (isHeaderViewPos(position) || isFooterViewPos(position)) {
                return;
            }
            holder.itemView.setTag(position - getHeadersCount());
            onMyBindViewHolder(holder, position - getHeadersCount());
        }
    }

    protected abstract void onMyBindViewHolder(BaseViewHolder holder, int position);

    /***
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    /***
     * 长按事件接口
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setItemLongClickListener(OnItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }

    public void getView() {

    }

    //-----------------------------
    public List<T> getList() {
        return mData;
    }

    /**
     * 设置数据
     */
    public void setData(List<T> data) {
        if (data == null) {
            return;
        }
        this.mData = data;
        notifyDataSetChanged();
    }

    public void add(T item) {
        mData.add(item);
        notifyItemInserted(mData.size() - 1);
    }

    public void add(int position, T item) {
        mData.add(position, item);
        notifyItemInserted(position);
    }

    public void addAll(List<T> list) {
        if (list != null) {
            int positon = mData.size() + getHeadersCount();
            mData.addAll(list);
            notifyItemRangeInserted(positon, list.size());
        }
    }

    public void setAll(List<T> list) {
        if (null == mData) {
            mData = new ArrayList<>();
        }
        if (null == list) {
            list = new ArrayList<>();
        }
        mData.clear();
        mData.addAll(list);
        notifyDataSetChanged();
    }

    public void removeLast() {
        if (null != mData && mData.size() > 0) {
            mData.remove(mData.size() - 1);
            notifyItemRemoved(mData.size() - 1);
        }
    }

    public void remove(int position) {
        mData.remove(position);
        // notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public void removeAll() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void update(int position, T item) {
        mData.remove(position);
        mData.add(position, item);
        notifyItemChanged(position);
    }

    /***
     * 刷新指定位置数据
     */
    public void updatePos(int position) {
        notifyItemChanged(position);
    }

    /***
     * 设置空视图
     */
    public void setEmptyView(int layoutResId, ViewGroup viewGroup) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
        setEmptyView(view);
    }


    /***
     * 判断当前位置是否是头部
     */
    private boolean isHeaderViewPos(int position) {
        return position < getHeadersCount();
    }

    /***
     * 判断当前位置是否是尾部
     */
    private boolean isFooterViewPos(int position) {
        return position >= getHeadersCount() + getRealItemCount();
    }

    /***
     * 设置空视图
     */
    public void setEmptyView(View emptyView) {
        boolean insert = false;
        if (mEmptyLayout == null) {
            mEmptyLayout = new FrameLayout(emptyView.getContext());
            final LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            final ViewGroup.LayoutParams lp = emptyView.getLayoutParams();
            if (lp != null) {
                layoutParams.width = lp.width;
                layoutParams.height = lp.height;
            }
            mEmptyLayout.setLayoutParams(layoutParams);
            insert = true;
        }
        mEmptyLayout.removeAllViews();
        mEmptyLayout.addView(emptyView);
        //mIsUseEmpty = true;
        if (insert) {
            notifyItemInserted(0);
        }
    }

    /***
     * 主视图数量
     */
    public int getRealItemCount() {
        if (StringsUtils.isEmpty(mData) && null != mEmptyLayout) {
            return 1;
        } else {
            //如果是图片选择器就+1
            if (clazz.toString().contains("ImageItem")) {
                return mData == null ? 1 : mData.size() + 1;
            } else {
                return mData == null ? 0 : mData.size();
            }
        }
        //return mData == null ? 0 : mData.size();
    }

    /***
     * 获取头部视图
     */
    public int getHeadersCount() {
        return mHeaderViews.size();
    }

    /***
     * 获取尾部视图
     */
    private int getFootersCount() {
        return mFootViews.size();
    }

    /***
     * 添加头部
     * @param view
     */
    public void addHeaderView(View view) {
        mHeaderViews.put(mHeaderViews.size() + HEADER_VIEW, view);
    }

    /***
     * 添加尾部
     * @param view
     */
    public void addFootView(View view) {
        mFootViews.put(mFootViews.size() + FOOTER_VIEW, view);
    }
}
